package com.pactera.madp.cp.controller.oms.product;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeAddDto;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeUpdateDto;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeEntity;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeGroupEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ProdEmployeeGroupService;
import com.pactera.madp.cp.service.ProdEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * 产品经理表(CpProdEmployee)表控制层
 *
 * @author yushen.qi
 * @since 2021-12-17 16:19:55
 */

@RestController
@AllArgsConstructor
@RequestMapping("/product-employee")
@Api(value = "product", tags = "产品经理管理")
public class ProdEmployeeController {

    /**
     * 服务对象
     */
    @Resource
    private ProdEmployeeService prodEmployeeService;
    @Resource
    private ProdEmployeeGroupService prodEmployeeGroupService;

    /**
     * 分页查询所有数据
     *
     * @param page           分页对象
     * @param name           查询条件
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation(value = "分页查询产品")
    public R selectAll(Page page, String name) {
        return R.ok(prodEmployeeService.getPage(page, name));
    }

    /**
     * 改变授权状态
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/change")
    @ApiOperation(value = "改变授权状态")
    public R change(@RequestParam Serializable id,@RequestParam Integer state) {
        return prodEmployeeService.update(null, new UpdateWrapper<ProdEmployeeEntity>().set("state",state).eq("id",id))?R.ok(null,"成功"):R.failed("失败");
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增数据")
    public R insert(@RequestBody ProductEmployeeAddDto dto) {
        ProdEmployeeEntity one = prodEmployeeService.getOne(Wrappers.<ProdEmployeeEntity>lambdaQuery().eq(ProdEmployeeEntity::getEmployeeId, dto.getEmployeeId()).eq(ProdEmployeeEntity::getCorpId, CorpContextHolder.getCorpId()));
        if(one!=null){
            throw new CheckedException("该产品经理已存在");
        }
        return prodEmployeeService.save(dto)?R.ok(null,"新增成功"):R.failed("新增失败");
    }

    /**
     * 修改数据
     *
     * @param dto 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改数据")
    public R update(@RequestBody ProductEmployeeUpdateDto dto) {
        return prodEmployeeService.put(dto)?R.ok(null,"修改成功"):R.failed("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除产品")
    public R delete(@RequestParam Integer id) {
        ProdEmployeeEntity one = prodEmployeeService.getOne(Wrappers.<ProdEmployeeEntity>lambdaQuery().eq(ProdEmployeeEntity::getId, id));
        if(one!=null){
            prodEmployeeGroupService.remove(Wrappers.<ProdEmployeeGroupEntity>lambdaQuery()
                    .eq(ProdEmployeeGroupEntity::getEmployeeId,one.getEmployeeId())
                    .eq(ProdEmployeeGroupEntity::getCorpId,one.getCorpId()));
        }
        return prodEmployeeService.remove(Wrappers.<ProdEmployeeEntity>lambdaQuery().eq(ProdEmployeeEntity::getId,id))?R.ok(null,"删除成功"):R.failed("删除失败");
    }

}
